package com.xxl.job.kettle.core.controller;

import com.github.pagehelper.PageInfo;
import com.xxl.job.kettle.core.entity.KScriptMonitor;
import com.xxl.job.kettle.core.entity.KScriptRecord;
import com.xxl.job.kettle.core.entity.QueryHelper;
import com.xxl.job.kettle.core.service.KScriptMonitorService;
import com.xxl.job.kettle.core.service.KScriptRecordService;
import com.xxl.job.kettle.result.Result;
import com.xxl.job.kettle.util.FileUtil;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 实现功能：
 *
 * @author 作者[zk]
 * @version 1.0.00  2022-11-29 21:15
 */
//@Api(tags = "监控管理")
@RequestMapping("/dataCollection/script/monitor")
@RestController
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class KScriptMonitorController extends BaseController<KScriptMonitor, KScriptMonitorService> {

    private final KScriptRecordService kScriptRecordService;
    /**
     * 根据条件查询转换监控列表
     *
     * @param req {@link QueryHelper}
     * @return {@link Result}
     */
//    @ApiOperation(value = "根据条件查询转换监控列表")
    @PostMapping("/findScriptMonitorListByPage")
    Result<PageInfo<KScriptMonitor>> findTransMonitorListByPage(@RequestBody QueryHelper<KScriptMonitor> req){
        PageInfo<KScriptMonitor> kScriptMonitors = service.findListByPage(req.getQuery(),req.getPage(),req.getRows());
        return Result.OK(kScriptMonitors);
    }
    /**
     * 查询转换id查询转换执行记录
     *
     * @param req 根据转换id查询
     * @return {@link Result}
     */
//    @ApiOperation(value = "查询转换监控明细")
    @PostMapping("/findScriptRecordList")
    Result<PageInfo<KScriptRecord>> findTransRecordList(@RequestBody QueryHelper<KScriptRecord> req){
        PageInfo<KScriptRecord> KScriptRecord = kScriptRecordService.findTransRecordList(req.getQuery(),req.getPage(),req.getRows());
        return Result.OK(KScriptRecord);
    }

    /**
     * 查看转换执行记录明细
     *
     * @param transRecordId 根据转换记录id查询
     * @return {@link Result}
     */
//    @ApiOperation(value = "查看转换执行记录明细")
    @GetMapping("/viewSriptRecordDetail")
    Result<String> viewTransRecordDetail(@RequestParam("transRecordId") String transRecordId){
        KScriptRecord record = kScriptRecordService.getById(transRecordId);
        String logContent = "";
        try {
            logContent = FileUtil.readFileToString(new File(record.getLogFilePath()), "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.OK(logContent.replace("\r\n", "<br/>"));
    }
    /**
     * 下载转换执行记录明细
     *
     * @param transRecordId 根据转换记录id查询
     */
//    @ApiOperation(value = "下载转换执行记录明细")
    @GetMapping("/downloadTransRecord")
    public void downloadTransRecord(HttpServletResponse response, String transRecordId) {
        // 查询文件路径
        KScriptRecord record = kScriptRecordService.getById(transRecordId);
        FileUtil.downloadFile(response, record.getLogFilePath());
    }
    /**
     * 对转换任务执行结果统计
     *
     * @return {@link Result}
     */
//    @ApiOperation(value = "对转换任务执行结果统计")
    @GetMapping("/countTrans")
    public Result<Map> countTrans() {
        return Result.OK(service.countTrans());
    }

    @GetMapping("/findErrorRecordList")
    Result<Object> findErrorRecordList(@RequestParam(value = "categoryId",required = false) String categoryId) {
        KScriptRecord kScriptRecord = new KScriptRecord();
        if(categoryId!=null && categoryId!=""){
            kScriptRecord.setCategoryId(categoryId);
        }
        List<KScriptRecord> kScriptRecords = kScriptRecordService.selectErrorList(kScriptRecord);
        return Result.OK(kScriptRecords);
    }
}
